package com.zyy.smartcity1129.hospital;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.PatientCardInfo;
import com.zyy.smartcity1129.data.PatientInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.data.UserInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.PpUtil;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;

/**
 * @ClassName: PatientFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 1/12/2021$
 * @Version: 1.0
 */
public class PatientFragment extends BaseFragment {
    private RecyclerView rvPatient;
    private ImageView imgPatientAdd;
    List<PatientInfo> patientList = new ArrayList<>();
    RvAd<PatientInfo> rvAd;
    private FrameLayout framePp2;
    private Button button4;
    private EditText editPPatient5;
    private EditText editPPatient4;
    private EditText editPPatientD;
    private EditText editPPatientM;
    private EditText editPPatientY;
    private EditText editPPatient2;
    private Spinner spPPatient1;
    private EditText editPPatient1;
    private FrameLayout framePp1;
    public static String patientName;

    @Override
    protected int layId() {
        return R.layout.f_patient;
    }

    @Override
    protected String title() {
        return "就诊人卡片";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        rvPatient.setLayoutManager(new LinearLayoutManager(getContext()));
        th(() -> {
            UserInfo.UserBean userBean = Http.getInstance().get("/prod-api/api/common/user/getInfo", null, UserInfo.class, 1).getUser();
            try {
                patientList.add(new PatientInfo(userBean.getNickName(), userBean.getSex(), (String) userBean.getIdCard(), userBean.getPhonenumber()));
            } catch (Exception e) {
                Log.e("TAG", "未设置名称");
                try {
                    patientList.add(new PatientInfo("王大卫", userBean.getSex(), (String) userBean.getIdCard(), userBean.getPhonenumber()));
                } catch (Exception e1) {
                    Log.e("TAG", "未设置性别");
                    try {
                        patientList.add(new PatientInfo("王大卫", "男", (String) userBean.getIdCard(), userBean.getPhonenumber()));
                    } catch (Exception e2) {
                        Log.e("TAG", "未设置身份证号");
                        try {
                            patientList.add(new PatientInfo("王大卫", "男", "1603537663212", userBean.getPhonenumber()));
                        } catch (Exception e3) {
                            Log.e("TAG", "未设置手机号");
                            patientList.add(new PatientInfo("王大卫", "男", "1603537663212", "13800000000"));
                        }
                    }
                }
            }
            ui(() -> {
                initAdapter(patientList);
                rvPatient.setAdapter(rvAd);
            });
        });
        th(() -> {
            try {
                List<PatientCardInfo.RowsBean> patientCards = Http.getInstance().get("/prod-api/api/hospital/patient/list", null, PatientCardInfo.class, 1).getRows();
                for (int i = 0; i < patientCards.size(); i++) {
                    patientList.add(new PatientInfo(patientCards.get(i).getId(), patientCards.get(i).getName(), patientCards.get(i).getSex(), patientCards.get(i).getCardId(), patientCards.get(i).getBirthday(), patientCards.get(i).getTel(), patientCards.get(i).getAddress()));
                }
                ui(() -> {
                    initAdapter(patientList);
                    rvPatient.setAdapter(rvAd);
                });
            } catch (Exception e) {
                Log.e("TAG", "候诊人卡片为空数据");
            }
        });
        imgPatientAdd.setOnClickListener(view -> {
            patientList.add(new PatientInfo(0, "", "", "", "", "", ""));
            initAdapter(patientList);
            rvPatient.setAdapter(rvAd);
        });
    }

    private void initAdapter(List<PatientInfo> patientList) {
        rvAd = new RvAd<PatientInfo>(patientList, R.layout.rv_base) {
            private void initView(View vi) {
                ig = (ImageView) vi.findViewById(R.id.ig);
                tv1 = (TextView) vi.findViewById(R.id.tv1);
                tv2 = (TextView) vi.findViewById(R.id.tv2);
                tv3 = (TextView) vi.findViewById(R.id.tv3);
                tv4 = (TextView) vi.findViewById(R.id.tv4);
                tv5 = (TextView) vi.findViewById(R.id.tv5);
                tv6 = (TextView) vi.findViewById(R.id.tv6);
                tv6.setVisibility(View.VISIBLE);
                lineRvPatient = (LinearLayout) vi.findViewById(R.id.lineRvPatient);
                imgRvPatient = (ImageView) vi.findViewById(R.id.imgRvPatient);
                ig.setVisibility(View.GONE);
            }

            private TextView tv6;
            private TextView tv5;
            private TextView tv4;
            private TextView tv3;
            private TextView tv2;
            private TextView tv1;
            private ImageView ig;
            private ImageView imgRvPatient;
            private LinearLayout lineRvPatient;

            @Override
            protected void initR(View vi, PatientInfo patientInfo, int p) {
                initView(vi);
                tv1.setText("姓名：" + patientInfo.getName());
                tv2.setText("性别：" + patientInfo.getSex());
                tv3.setText("身份证号：" + patientInfo.getIdCard());
                tv4.setText("出生日期：" + patientInfo.getBirthday());
                tv5.setText("手机号：" + patientInfo.getPhonenumber());
                tv6.setText("地址：" + patientInfo.getAddress());
                imgRvPatient.setVisibility(View.VISIBLE);
                imgRvPatient.setOnClickListener(view -> {
                    patientName = patientInfo.getName();
                    add(new DepartmentFragment());
                });
                if (patientInfo.getId() == 0) {
                    Log.i("TAG", "new");
                    lineRvPatient.setOnClickListener(view -> {
                        initPp(0, 0);
                    });
                } else {
                    lineRvPatient.setOnClickListener(view -> {
                        initPp(1, patientInfo.getId());
                    });
                }
            }
        };
    }

//    0 new 1 revise
    private void initPp(int i, int id) {
        PpUtil ppUtil = new PpUtil();
        View v = ppUtil.getP(requireContext(), R.layout.p_patient);
        framePp1 = (FrameLayout) v.findViewById(R.id.frame_pp1);
        editPPatient1 = (EditText) v.findViewById(R.id.editPPatient1);
        spPPatient1 = (Spinner) v.findViewById(R.id.spPPatient1);
        editPPatient2 = (EditText) v.findViewById(R.id.editPPatient2);
        editPPatientY = (EditText) v.findViewById(R.id.editPPatientY);
        editPPatientM = (EditText) v.findViewById(R.id.editPPatientM);
        editPPatientD = (EditText) v.findViewById(R.id.editPPatientD);
        editPPatient4 = (EditText) v.findViewById(R.id.editPPatient4);
        editPPatient5 = (EditText) v.findViewById(R.id.editPPatient5);
        button4 = (Button) v.findViewById(R.id.button4);
        framePp2 = (FrameLayout) v.findViewById(R.id.frame_pp2);
        framePp1.setOnClickListener(v1 -> {
            ppUtil.closeP();
        });
        framePp2.setOnClickListener(v1 -> {
            ppUtil.closeP();
        });
        button4.setOnClickListener(v1 ->{
            String name = editPPatient1.getText().toString().trim();
            String sex = spPPatient1.getSelectedItem().toString();
            String rS = "";
            if (sex.equals("男")) {
                rS = "0";
            } else if (sex.equals("女")) {
                rS = "1";
            }
            String carId = editPPatient2.getText().toString();
            int Y = Integer.parseInt(editPPatientY.getText().toString());
            int M = Integer.parseInt(editPPatientM.getText().toString());
            int D = Integer.parseInt(editPPatientD.getText().toString());
            String mM = "";
            String mD = "";
            if (M < 10 && M > 0) {
                mM = "0" + M;
            } else if (M < 1){
                mM = "01";
            } else {
                mM = String.valueOf(M);
            }
            if (D < 10 && D > 0) {
                mD = "0" + D;
            } else if (D < 1) {
                mD = "01";
            } else {
                mD = String.valueOf(D);
            }
            String birthday = Y + "-" + mM + "-" + mD;
            String phoneNumber = editPPatient4.getText().toString();
            String address = editPPatient5.getText().toString();
            if (!name.equals("") && !rS.equals("") && !carId.equals("") && !birthday.equals("") && !phoneNumber.equals("") && !address.equals("")) {
                String finalRS = rS;
                th(() ->{
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("address", address);
                    jsonObject.addProperty("birthday", birthday);
                    jsonObject.addProperty("cardId", carId);
                    jsonObject.addProperty("name", name);
                    jsonObject.addProperty("sex", finalRS);
                    jsonObject.addProperty("tel", phoneNumber);
                    String result = "";
                    if (i == 0) {
                        result = Http.getInstance().get("/prod-api/api/hospital/patient", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                    } else if (i == 1) {
                        jsonObject.addProperty("id", id);
                        result = Http.getInstance().get("/prod-api/api/hospital/patient", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 3).getMsg();
                    }
                    String finalResult = result;
                    if (result.equals("操作成功")) {
                        th(() ->{
                            List<PatientCardInfo.RowsBean> patientCards = Http.getInstance().get("/prod-api/api/hospital/patient/list", null, PatientCardInfo.class, 1).getRows();
                            patientList.clear();
                            for (int y = 0; y < patientCards.size(); y++) {
                                patientList.add(new PatientInfo(patientCards.get(y).getId(), patientCards.get(y).getName(), patientCards.get(y).getSex(), patientCards.get(y).getCardId(), patientCards.get(y).getBirthday(), patientCards.get(y).getTel(), patientCards.get(y).getAddress()));
                            }
                            ui(() ->{
                                initAdapter(patientList);
                                rvPatient.setAdapter(rvAd);
                            });
                        });
                    }
                    ui(()->{
                        Log.i("TAG11", finalResult);
                        Toast.makeText(getContext(), finalResult, Toast.LENGTH_SHORT).show();
                    });
                });
                ppUtil.closeP();
            } else {
                Toast.makeText(getContext(), "数据待补充", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView(View iv) {
        rvPatient = (RecyclerView) iv.findViewById(R.id.rvPatient);
        imgPatientAdd = (ImageView) iv.findViewById(R.id.imgPatientAdd);
    }
}
